<template>
  <div class="contactsList">
    <div class="li" v-for="item in contentList" v-if="contentList">
      <div>手机号：<span>{{item.mobile}}</span></div>
      <div>归属地：<span>{{item.numberLocation}}</span></div>
      <div>拨打时间：<span>{{item.callTime}}</span></div>
      <div>拨打时长：<span>{{item.talkTime}}</span></div>
      <div>类型：<span>{{item.type}}</span></div>
    </div>
    <div class="centent" v-if="!contentList"><i>您暂未通话记录哟！</i></div>
  </div>
</template>

<script>
  export default {
    name: "contacts-list",
    data() {
      return {
        contentList: ""
      }
    },
    mounted() {
      const that = this;
      that.getContacs();
    },
    methods: {
      getContacs() { // 获取手机通话记录
        const that = this;
        try {
          var CallLog = plus.android.importClass("android.provider.CallLog");
          var main = plus.android.runtimeMainActivity();
          var ContentResolver = plus.android.importClass('android.content.ContentResolver');
          var obj = main.getContentResolver();
          plus.android.importClass(obj);
          //查询
          var cursor = obj.query(
            CallLog.Calls.CONTENT_URI,
            null,
            null,
            null,
            CallLog.Calls.DEFAULT_SORT_ORDER
          );
          plus.android.importClass(cursor);
          var content = []; // 用来存储数据
          var count = 0; // 记录多少条
          if (cursor.moveToFirst()) {
            while (cursor.moveToNext()) {
              count++;
              //号码
              var number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
              //呼叫类型
              var type;
              switch (
                parseInt(cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)))) // 判断通话类型
              {
                case CallLog.Calls.INCOMING_TYPE:
                  type = "呼入";
                  break;
                case CallLog.Calls.OUTGOING_TYPE:
                  type = "呼出";
                  break;
                case CallLog.Calls.MISSED_TYPE:
                  type = "未接";
                  break;
                default:
                  type = "挂断"; //应该是挂断.根据我手机类型判断出的
                  break;
              }
              // 获取时间
              var date = new Date(parseInt(
                cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DATE))));
              // 联系人
              var Name_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME);
              var name = cursor.getString(Name_Col) ? cursor.getString(Name_Col) : "未知";
              // 号码归属地 返回：北京 联通
              var numberLocation = cursor.getString(
                cursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION)
              ) ? cursor.getString(
                cursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION)
              ) : "未知";
              //通话时间,单位:s
              var Duration_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION);
              var duration = cursor.getString(Duration_Col);
              // 存入数组
              content.push({
                name: name, // 联系人的姓名
                mobile: number, // 联系人电话
                numberLocation: numberLocation, // 号码的归属地
                callTime: that.getDate(date), // 呼入或呼出时间
                talkTime: duration + 's', // 通话时长
                type: type
              });
              // 查询100条 就跳出
              if (count > 100) {
                break;
              }
            }
          }
          that.contentList = content;
        } catch {
          that.contentList="";
          console.log("出现错误");
        }
      },
      removeEmoji($text) {
        var regRule = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
        if ($text.match(regRule)) {
          $text = $text.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
        }
        return $text
      },
      getDate(str) {
        var oTime = "";
        var oDate = new Date(Number(str)),
          oYear = oDate.getFullYear(),
          oMonth = oDate.getMonth() + 1,
          oDay = oDate.getDate(),
          oHour = oDate.getHours(),
          oMin = oDate.getMinutes(),
          oSec = oDate.getSeconds();
        oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSec); //最后拼接时间
        function getzf(num) { //补0操作
          if (parseInt(num) < 10) {
            num = '0' + num;
          }
          return num;
        }

        return oTime;
      }
    }
  }
</script>

<style scoped lang="scss">
  .contactsList {
    width: 100%;
    height: 100%;
    padding: 0 16px;
    .li {
      width: 100%;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      background: white;
      margin-top: 5px;
      padding: 15px;
      border-radius: 15px;

      div {
        min-width: 50%;
        height: 40px;
        font-size: 14px;
        color: #000;
        line-height: 40px;

        span {
          color: red;
          font-size: 14px;
          display: inline-block;
          margin-left: 5px;
        }

      }
    }
    .centent{
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
</style>
